package com.zxc.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.zxc.reggie.common.R;
import com.zxc.reggie.dto.DishDto;
import com.zxc.reggie.entity.Category;
import com.zxc.reggie.entity.Dish;
import com.zxc.reggie.entity.DishFlavor;
import com.zxc.reggie.service.CategoryService;
import com.zxc.reggie.service.DishFlavorService;
import com.zxc.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Resource
    private DishService dishService;

    @Resource
    private CategoryService categoryService;

    @Resource
    private DishFlavorService dishFlavorService;

    @PostMapping()
    public R<String> save(@RequestBody DishDto dishDto) {

        return dishService.savaWithFlavor(dishDto);

    }

    @GetMapping("/page")
    public R<Page<DishDto>> page(int page, int pageSize, String name) {

        Page<Dish> dishPage = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();

        dishLambdaQueryWrapper.like(name != null, Dish::getName, name);
        dishLambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(dishPage, dishLambdaQueryWrapper);
        BeanUtils.copyProperties(dishPage, dishDtoPage, "records");
        List<Dish> dishPageRecords = dishPage.getRecords();

        List<DishDto> dishDtoList = dishPageRecords.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(dishDtoList);

        return R.success(dishDtoPage);
    }

    @GetMapping("/{id}")
    public R<DishDto> findDishDto(@PathVariable String id) {

        Dish dish = dishService.getById(id);
        Long categoryId = dish.getCategoryId();
        Category category = categoryService.getById(categoryId);
        Long dishId = dish.getId();

        LambdaQueryWrapper<DishFlavor> DishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        DishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishId);
        DishFlavorLambdaQueryWrapper.orderByDesc(DishFlavor::getUpdateTime);
        DishDto dishDto = new DishDto();
        List<DishFlavor> dishFlavors = dishFlavorService.list(DishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(dishFlavors);
        String categoryName = category.getName();

        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setCategoryName(categoryName);
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {

        return dishService.updateWithFlavor(dishDto);

    }

    @GetMapping("/list")
    public R<List<DishDto>> findDish(Dish dish) {

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId()).eq(Dish::getStatus, 1).orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(dishLambdaQueryWrapper);
        List<DishDto> dishDtoList= list.stream().map(item->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            List<DishFlavor> dishFlavors = dishFlavorService.query().eq("dish_id", item.getId()).list();
            dishDto.setFlavors(dishFlavors);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtoList);
    }

    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable String status, @RequestBody Long ids) {
        dishService.update().eq("id", ids).set("status", status);
        return R.success("修改成功");
    }
}
